package com.appheader.rongmaphone.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;

import com.appheader.rongmaphone.cache.preference.PreferenceUtil;
import com.appheader.rongmaphone.constants.GlobalVars;
import com.appheader.rongmaphone.user.UserInfo;
import com.appheader.rongmaphone.utils.json.JSONUtil;

/**
 * Created by lpc on 16/9/1.
 */
public class DbUtil {
    /**
     * @param picModel
     * insert pic
     */
    private PreferenceUtil preferenceUtil;
    private static String uid;//用户uid
    private Context context;
    MyOpenHelper openHelper;

    public DbUtil(Context context) {
        this.context = context;
        openHelper = new MyOpenHelper(context);
    }

    public Cursor selectLastModifyTime2(String cloudId){
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db.rawQuery("select * from " + buildPicTableName() + " where "+DBConst.CloudId+"=?", new String[]{cloudId});
    }
    /**
     * 表名，
//     * @param picModel
     */
//    public  void insertNativePicInfo(CloudEntity picModel){
//        SQLiteDatabase dbwrite = openHelper.getWritableDatabase();
//       dbwrite.insert(buildPicTableName(),null,picModel.toContentValues(false));
//    }

//    public  static  void insertNativePicInfo(CloudEntity picModel){
////        SQLiteDatabase dbwrite = openHelper.getWritableDatabase();
//        DBManager.getDB().insert(buildPicTableName(),null,picModel.toContentValues(false));
//    }
//    public static CloudEntity selectLastModifyTime(String cloudId){
//        CloudEntity picModel = null;
//        Cursor cursor = DBManager.getDB().rawQuery("select * from " + buildPicTableName() + " where "+DBConst.CloudId+"=?", new String[]{cloudId});
//        if (cursor.moveToFirst()) {
//            picModel = CloudEntity.fromCursor(cursor);
//        }
//        cursor.close();
//        return  picModel;
//    }

    public static String buildPicTableName(){
        PreferenceUtil preferenceUtil = new PreferenceUtil(GlobalVars.getContext());
        boolean isFirst = preferenceUtil.getBoolean("isFirst");
        String userStr = preferenceUtil.getString("userStr");

        Log.e("TAG", "getHeaderRequest: "+isFirst+"++++++"+userStr+"-------");


        if (!TextUtils.isEmpty(userStr)) {
            UserInfo userInfos = JSONUtil.userInfos(userStr);
            uid = userInfos.getUid();
        }
        return DBConst.TABLE_CLOUD+DBConst.TABLE_LINK+uid;
    }

//    public static List<String> selectUpLoadPic(){
//        ArrayList<String> list = new ArrayList<>();
//        Cursor cursor = DBManager.getDB().rawQuery("select * from " + buildPicTableName() + " where pic_isupload=1", null);
//        if (cursor.moveToNext()) {
//            list.add(CloudEntity.fromCursor(cursor).getPicId()) ;
//        }
//        return list;
//    }
//    public static List<CloudEntity> testSelectUpLoadPic(){
//        ArrayList<CloudEntity> list = new ArrayList<>();
//        Cursor cursor = DBManager.getDB().rawQuery("select * from " + buildPicTableName() , null);
//        if (cursor.moveToNext()) {
//            list.add(CloudEntity.fromCursor(cursor)) ;
//        }
//        return list;
//    }
}
